package com.lkyh.pad;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * A login screen that offers login via email/password.
 */
public class SettingActivity extends AppCompatActivity {

    public final static String FILE_NAME = "pda.txt";
    //public final static String DEFAULT_URL = "http://192.168.0.35:8080/pda";
    public final static String DEFAULT_URL = "http://118.126.99.243:16066/pda-webapp";


    private EditText urlText;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        checkPermission();
        // Set up the login form.
        urlText = findViewById(R.id.url);
        String url = FileUtil.getFile(FILE_NAME);
        if (url == null || "".equals(url)) {
            url = DEFAULT_URL;
            FileUtil.saveFile(url, FILE_NAME);
        }
        urlText.setText(url);

        Button stbtn = findViewById(R.id.stbtn);
        stbtn.setOnClickListener((view) -> {
            if (isUrl(urlText.getText().toString())) {
                //按下确定键后的事件
                //Toast.makeText(getApplicationContext(), et.getText().toString(), Toast.LENGTH_LONG).show();
                FileUtil.saveFile(urlText.getText().toString(), FILE_NAME);
                SettingActivity.this.finish();
            } else {
                Toast.makeText(getApplicationContext(), "地址不是合法的URL", Toast.LENGTH_LONG).show();
            }

        });

        Button dbtn = findViewById(R.id.dbtn);
        dbtn.setOnClickListener((view) -> {
            urlText.setText(DEFAULT_URL);

        });


    }

    private void checkPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            int checkCallPhonePermission = ContextCompat.checkSelfPermission(SettingActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if (checkCallPhonePermission != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(SettingActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 333);
                return;
            } else {

            }
        } else {

        }
    }

    private boolean isUrl(String url) {

        String strRegex = "^((https|http|ftp|rtsp|mms)?://)"
                + "?(([0-9a-z_!~*\'().&=+$%-]+: )?[0-9a-z_!~*\'().&=+$%-]+@)?" //ftp的user@
                + "(([0-9]{1,3}.){3}[0-9]{1,3}" // IP形式的URL- 199.194.52.184
                + "|" // 允许IP和DOMAIN（域名）
                + "([0-9a-z_!~*\'()-]+.)*" // 域名- www.
                + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]." // 二级域名
                + "[a-z]{2,6})" // first level domain- .com or .museum
                + "(:[0-9]{1,4})?" // 端口- :80
                + "((/?)|" // a slash isn't required if there is no file name
                + "(/[0-9a-z_!~*\'().;?:@&=+$,%#-]+)+/?)$";


        if (url.matches(strRegex)) {
            return true;
        } else {
            return false;
        }

    }
}

